package com.reggielcy.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * @auther teacher
 * @date 2024/3/27
 * @project reggie_take_out
 */

@ControllerAdvice(annotations ={RestController.class, Controller.class})
@ResponseBody
@Slf4j
public class ExceptionHandler {
    @org.springframework.web.bind.annotation.ExceptionHandler
    public R<String> exceptionHandler(SQLIntegrityConstraintViolationException ex){
        log.info("注册失败");
        log.error(ex.getMessage());
        if (ex.getMessage().contains("Duplicate entry")){
            String[] split = ex.getMessage().split(" ");
            String admin=split[2];
            return R.error(admin+"账户已被注册");
        }
        return R.error("出错了");
    }
    @org.springframework.web.bind.annotation.ExceptionHandler
    public R<String> exceptionHandler1(CustomException ex){
        return R.error(ex.getMessage());
    }


}
